//
//  Kylin_PostWordViewController.m
//  03-BuDeJie
//
//  Created by Kylin on 16/2/24.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_PostWordViewController.h"
#import "Kylin_PlaceholderTextView.h"
#import "Kylin_PostWordToolbar.h"

@interface Kylin_PostWordViewController ()<UITextViewDelegate>
/**textView*/
@property(nonatomic,weak) Kylin_PlaceholderTextView *textView;

/**toolbar*/
@property(nonatomic,weak)Kylin_PostWordToolbar *toolbar;
@end

@implementation Kylin_PostWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
  
    [self setupTextView];
    
    [self setupToolbar];
    
}
/*
 // 重新刷新自己和子控件的所有内容（状态、尺寸）
 [tempView layoutIfNeeded];
 // 重新调用tempView的layoutSubviews（重新排布子控件的frame）
 [tempView setNeedsLayout];
 // 重新调用tempView的drawRect:方法（重新绘制tempView里面的内容，一般不包括子控件）
 [tempView setNeedsDisplay];
 */

- (void)setupToolbar{
    Kylin_PostWordToolbar *toolbar = [Kylin_PostWordToolbar viewForXib];
    toolbar.x = 0;
    toolbar.y = self.view.height - toolbar.height;
    toolbar.width = self.view.width;
    [self.view addSubview:toolbar];
    self.toolbar = toolbar;
    
    // 成为键盘辅助控件
//    self.textView.inputAccessoryView = toolbar;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}
#pragma mark- 设置导航栏
- (void)setupNav{
    self.title = @"发表文字";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发表" style:UIBarButtonItemStyleDone target:self action:@selector(post)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    [self.navigationController.navigationBar layoutIfNeeded];
}
#pragma mark- 设置textView
- (void)setupTextView{
    
    Kylin_PlaceholderTextView *textView = [[Kylin_PlaceholderTextView alloc]init];
    textView.frame = self.view.bounds;
    textView.alwaysBounceVertical = YES;
    textView.delegate = self;
    textView.placeholder = @"把好玩的图片，好笑的段子或糗事发到这里，接受千万网友膜拜吧！发布违反国家法律内容的，我们将依法提交给有关部门处理。";
    [self.view addSubview:textView];
    
    self.textView = textView;
}
- (void)post{
    Kylin_LogFunc;
    
}

#pragma mark- 监听方法

- (void)keyboardWillChangeFrame:(NSNotification *)note{
    
//    Kylin_Log(@"%@",note);
    
    // 取出改变持续时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        // 工具条平移的距离 == 键盘最终的Y值 - 屏幕的高度
        CGFloat ty = [note.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue].origin.y - Kylin_ScreenH;
        self.toolbar.transform = CGAffineTransformMakeTranslation(0, ty);
    }];
}
- (void)cancel{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark- <UItextViewDelegate>
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

- (void)textViewDidChange:(UITextView *)textView{
    self.navigationItem.rightBarButtonItem.enabled = textView.hasText;
}
@end
